package cn.edu.xaut.mybatisers.mall.Controller;

import cn.edu.xaut.mybatisers.mall.Entity.AReturnDataStruct;
import cn.edu.xaut.mybatisers.mall.Entity.Commodity;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.ParametersIncompleteException;
import cn.edu.xaut.mybatisers.mall.Service.ConsumerCommodityService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/consumer")
public class ConsumerCommodityController {
    @Autowired
    ConsumerCommodityService consumerCommodityService;
    
    @ApiOperation("条件查询商品")
    @RequestMapping(value = "/findCommodity", method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct findCommodity(
            Commodity commodity,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize) throws SQLException, IOException {
        PageInfo<Commodity> data = consumerCommodityService.findCommodity(commodity, pageNum, pageSize);
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage("查询成功");
        return_data.setData(data);
        return return_data;
    }

    @ApiOperation("查询商品评论")
    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct getCommodityComment(
            Commodity commodity,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize
    ) throws ParametersIncompleteException, SQLException {
        if (commodity.getId() <= 0){
            throw new ParametersIncompleteException("商品id不完整");
        }
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setData(consumerCommodityService.getCommodityComment(commodity, pageNum, pageSize));
        return_data.setResult(true);
        return_data.setCode(200);
        return return_data;
    }
}
